<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateFreightRequest;
use App\Http\Requests\UpdateFreightRequest;
use App\Repositories\FreightRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class FreightController extends AppBaseController
{
    /** @var  FreightRepository */
    private $freightRepository;

    public function __construct(FreightRepository $freightRepo)
    {
        $this->freightRepository = $freightRepo;
    }

    /**
     * Display a listing of the Freight.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->freightRepository->pushCriteria(new RequestCriteria($request));
        $freights = $this->freightRepository->all();

        return view('freights.index')
            ->with('freights', $freights);
    }

    /**
     * Show the form for creating a new Freight.
     *
     * @return Response
     */
    public function create()
    {
        return view('freights.create');
    }

    /**
     * Store a newly created Freight in storage.
     *
     * @param CreateFreightRequest $request
     *
     * @return Response
     */
    public function store(CreateFreightRequest $request)
    {
        $input = $request->all();

        $freight = $this->freightRepository->create($input);

        Flash::success('Freight saved successfully.');

        return redirect(route('freights.index'));
    }

    /**
     * Display the specified Freight.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $freight = $this->freightRepository->findWithoutFail($id);

        if (empty($freight)) {
            Flash::error('Freight not found');

            return redirect(route('freights.index'));
        }

        return view('freights.show')->with('freight', $freight);
    }

    /**
     * Show the form for editing the specified Freight.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $freight = $this->freightRepository->findWithoutFail($id);

        if (empty($freight)) {
            Flash::error('Freight not found');

            return redirect(route('freights.index'));
        }

        return view('freights.edit')->with('freight', $freight);
    }

    /**
     * Update the specified Freight in storage.
     *
     * @param  int              $id
     * @param UpdateFreightRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateFreightRequest $request)
    {
        $freight = $this->freightRepository->findWithoutFail($id);

        if (empty($freight)) {
            Flash::error('Freight not found');

            return redirect(route('freights.index'));
        }

        $freight = $this->freightRepository->update($request->all(), $id);

        Flash::success('Freight updated successfully.');

        return redirect(route('freights.index'));
    }

    /**
     * Remove the specified Freight from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $freight = $this->freightRepository->findWithoutFail($id);

        if (empty($freight)) {
            Flash::error('Freight not found');

            return redirect(route('freights.index'));
        }

        $this->freightRepository->delete($id);

        Flash::success('Freight deleted successfully.');

        return redirect(route('freights.index'));
    }
}
